package lambda.demo01;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @author Mr.ma
 * @since 2022/11/3 14:44
 * 因为lambda具有只关注抽象方法的特性，
 * 所以jdk在java.until.function包下提供了四个官方函数式接口方便开发
 *      1、Consumer<T>：消费型函数式接口，有参无返回值，抽象方法为void accept(T t)
 *      2、Supplier<T>：供给型函数式接口，无参有返回值，抽象方法为T get()
 *      3、Function<T,R>：函数型函数式接口，有参有返回值，抽象方法为R apply(T t)
 *      4、Predicate<T>：断言型函数式接口，有参有返回值，返回值为布尔类型，抽象方法为boolean test(T t)
 **/
public class TestLambda02 {
    public static void main(String[] args) {
        List list = Arrays.asList(new Integer[]{1, 3, 2, 7, 5, 9, 6, 8});
        doSome(x->System.out.println("Consumer参数 ====== " + x));
        doSome(()->new Student("张三",11));
        doOther(x -> x,list);
        doOther(x -> x.getAge() > 18,new Student("李四",12));
    }
    public static void doSome(Consumer consumer){
        consumer.accept(11);
    }
    public static void doSome(Supplier supplier){
        Object o = supplier.get();
        System.out.println("Supplier返回值 ===== " + o);
    }
    public static void doOther(Function<List,List> function, List list){
         List apply = function.apply(list);
         System.out.println("==========Function==========");
         apply.stream().forEach(System.out::println);
        System.out.println("==========Function==========");
    }
    public static void doOther(Predicate<Student> predicate,Student student){
        boolean test = predicate.test(student);
        if (test){
            System.out.println(student.getName() + "满足要求");
        } else {
            System.out.println(student.getName() + "不满足要求");
        }
    }
}
